<?php
namespace App\Manager;
use App\Manager\Storage;

/**
 * 房间管理类
 * 房间使用SETS
 */
class RoomManager extends BaseManager
{
    const PER_FIX = 'ROOM:';

    const MAP_PER_FIX = 'ROOM:MAP:';

    const NOWPAGE_PER_FIX = 'ROOM:NOWPAGE:';

    /*
        维护同一个id下的
    */
    const ROOM_KEY = '';
    /**
     * 进入房间
     * @param key string
     * @param df int   用户的链接id
     * @param result bool
     */
    public function enter($id, $page, $df)
    {
        $res1 = $this->getStorage()->sadd(self::PER_FIX . $id, $df.'');
        $res2 = $this->getStorage()->sadd(self::PER_FIX . $id . '-' . $page, $df.'');
        $res3 = $this->createMap($id . '-' . $page, $df.'');
        return  false !== $res1 && false !== $res2 && false !== $res3;
    }

    /**
     * 离开房间
     * @param key string
     * @param df int   用户的链接id
     * @param result bool
     */
    public function leave($key, $df)
    {
        return false !== $this->getStorage()->srem(self::PER_FIX . $this->getId($key), $df.'')
                    && false !== $this->getStorage()->srem(self::PER_FIX . $key, $df.'')
                    && false !== $this->removeMap($df.'');
    }

    /**
     * 获取房间里面所有除XX以外的人
     * @param key string
     * @return data array 
     */
    public function members($key, $except = [])
    {
        $list = $this->getStorage()->smembers(self::PER_FIX . $key);
        return is_array($list) ? array_diff($list, $except) : [];
    }

    public function roomMembers($key, $except = [])
    {
        $list = $this->getStorage()->smembers(self::PER_FIX . $this->getId($key));
        return is_array($list) ? array_diff($list, $except) : [];
    }

    /**
     * 用户房间对应hash表
     */
    public function createMap($key, $df)
    {
        return $this->getStorage()->hset(self::MAP_PER_FIX . 'HASH_MAP', $df.'', $key);
        // return $this->getStorage()->set(self::MAP_PER_FIX . 'SET_MAP:' . $df, $key);
    }

    public function map($df)
    {
        return $this->getStorage()->hget(self::MAP_PER_FIX . 'HASH_MAP', $df.'');
        // return $this->getStorage()->get(self::MAP_PER_FIX . 'SET_MAP:' . $df);
    }

    public function removeMap($df)
    {
        return $this->getStorage()->hdel(self::MAP_PER_FIX . 'HASH_MAP', $df.'');
        // return $this->getStorage()->remove(self::MAP_PER_FIX . 'SET_MAP:' . $df);
    }

    public function allMap()
    {
        return $this->getStorage()->hkeys(self::MAP_PER_FIX . 'HASH_MAP');
    }

    /**
     * [
     *      page => 1
     *      static => 0, // 0 表示没有动画， 其他表示动画页码
     * ]
     * 设置当前演讲的页码 TODO
     */
    public function setNowPage($id, $page)
    {
        return $this->getStorage()->set(self::NOWPAGE_PER_FIX . $id, json_encode($page));
    }

    /**
     * 获取当前演讲的页码 TODO
     */
    public function getNowPage($id)
    {
        $res = json_decode($this->getStorage()->get(self::NOWPAGE_PER_FIX . $id, json_encode(['page' => 1, 'animate' => 0])), true);
        return $res ? $res : ['page' => 1, 'animate' => 0];
    }

    public function getId($key)
    {
        return explode('-', $key)[0];
    }

}